package com.ebt.m.users;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.buscomponent.listview.pulltorefresh.Utils;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.users.bean.CardHonour;
import com.ebt.m.utils.ConfigData;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class CardHonourView extends IView {


    @BindView(R.id.honour_title)
    TextView honourTitle;
    @BindView(R.id.honour_date)
    TextView honourDate;
    @BindView(R.id.honour_content)
    TextView honourContent;
    @BindView(R.id.honour_img)
    ImageView honourImg;
    @BindView(R.id.item_root)
    LinearLayout itemRoot;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.header_divider)
    View headerDivider;
    String tag;

    public CardHonourView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_carddata_honour, this);
        ButterKnife.bind(this);
    }

    public CardHonourView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CardHonourView(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... obj) {
        try {
            ll.post(() -> {
                CardHonour cardHonour = (CardHonour) obj[0];
                if (obj != null && obj.length > 1 && obj[1] != null) {
                    Boolean hideHeaderDivider = (Boolean) obj[1];
                    headerDivider.setVisibility(hideHeaderDivider ? View.INVISIBLE : View.VISIBLE);
                } else {
                    headerDivider.setVisibility(View.VISIBLE);
                }
                honourTitle.setText(cardHonour.getTitle());
                honourDate.setText(cardHonour.getDate());
                honourContent.setText(cardHonour.getDescription());
                tag = cardHonour.getImgAddress() + mPosition;
                if (ll.getWidth() != 0)
                    ConfigData.CARD_HONOUR_IMG_WIDTH = ll.getWidth();
                honourImg.setTag(tag);
                Glide.with(getContext())
                        .load(cardHonour.getImgAddress())
                        .asBitmap()
                        .listener(new RequestListener<String, Bitmap>() {
                            @Override
                            public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                                honourImg.setImageResource(R.drawable.empty_photo);
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                                return false;
                            }
                        })
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                int width = resource.getWidth();
                                int height = resource.getHeight();
                                if (tag.equals((String) honourImg.getTag())) {
                                    Observable.just("")
                                            .map(ss -> {
                                                float scale = 1;
                                                if (width > height) {
                                                    scale = (float) ConfigData.CARD_HONOUR_IMG_WIDTH / width;
                                                } else {
                                                    scale = (float) Utils.convertDpToPixel(getContext(), 236) / height;
                                                }
                                                return Bitmap.createScaledBitmap(resource, (int) (width * scale), (int) (height * scale), true);
                                            })
                                            .subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(bitmap -> honourImg.setImageBitmap(bitmap)
                                                    , throwable -> {
                                                        Logger.e(throwable.getMessage());
                                                        honourImg.setImageResource(R.drawable.empty_photo);
                                                    });
                                }
                            }
                        });
            });
        } catch (Exception e) {
            Logger.e(e);
        }

    }

}
